import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_boost/boost_navigator.dart';
import 'package:flutter_module/common/base/base_page.dart';
import 'package:flutter_module/common/utils/object_to_any.dart';
import 'package:flutter_module/common/utils/toast_utils.dart';
import 'package:flutter_module/common/widget/CustomDialog.dart';
import 'package:flutter_module/page/base/base_view.dart';
import 'package:flutter_module/page/base/image_page.dart';
import 'package:flutter_module/page/base/max_input_edit_text_page.dart';
import 'package:flutter_module/style/colors.dart';

/// 合作服务商--banner
class ServiceProviderEditCaseShowPage extends BaseStateFulWidget {
  ServiceProviderEditCaseShowPage({required this.params});

  final int maxCount = 12;
  final Map params;

  @override
  BaseState<StatefulWidget> buildState() {
    return _ServiceProviderEditCaseShowState();
  }
}

class _ServiceProviderEditCaseShowState
    extends BaseState<ServiceProviderEditCaseShowPage> with BasePage {
  var imgList = <String>[];
  bool saveClick = false;
  String title = "";
  String content = "";
  final _divider_10 = SizedBox(
    height: 10,
  );

  var keyTitle = GlobalKey<MaxInputEditTextPageState>();
  var keyContent = GlobalKey<MaxInputEditTextPageState>();

  @override
  void initState() {
    title = widget.params["title"] ?? "";
    content = widget.params["content"] ?? "";
    imgList = object2string(widget.params["images"])
        .split(',')
        .where((e) => e.isNotEmpty)
        .toList();
    super.initState();
  }

  @override
  String get appBarTitle => "案例展示";

  @override
  getActions() {
    return [
      new TextClickView(
        title: "保存",
        isTitleRight: true,
        rightClick: () {
          _onSubmit();
        },
      )
    ];
  }

  @override
  Widget createView(
    BuildContext context,
  ) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 16, top: 13, right: 16),
      child: ListView(
        shrinkWrap: true,
        children: <Widget>[
          Text.rich(
            TextSpan(
              style: TextStyle(color: ThemeColors.text333, fontSize: 14),
              children: <InlineSpan>[
                TextSpan(
                    text: "*",
                    style: TextStyle(color: ThemeColors.text_ef5d5e)),
                TextSpan(text: "案例标题")
              ],
            ),
          ),
          _divider_10,
          MaxInputEditTextPage(
            key: keyTitle,
            content: title,
            hintText: "请输入案例标题",
            maxLength: 120,
            showLine: 1,
          ),
          _divider_10,
          Text.rich(
            TextSpan(
              style: TextStyle(color: ThemeColors.text333, fontSize: 14),
              children: <InlineSpan>[
                TextSpan(
                    text: "*",
                    style: TextStyle(color: ThemeColors.text_ef5d5e)),
                TextSpan(text: "案例描述")
              ],
            ),
          ),
          _divider_10,
          MaxInputEditTextPage(
            key: keyContent,
            content: content,
            hintText: "请输入案例展示文案",
            maxLength: 100,
            showLine: 5,
          ),
          _divider_10,
          Text.rich(
            TextSpan(
              style: TextStyle(color: ThemeColors.text333, fontSize: 14),
              children: <InlineSpan>[
                TextSpan(
                    text: "*",
                    style: TextStyle(color: ThemeColors.text_ef5d5e)),
                TextSpan(text: "案例详情")
              ],
            ),
          ),
          _divider_10,
          Expanded(child: ImagePage(imgList, widget.maxCount)),
        ],
      ),
    );
  }

  @override
  onBack() async {
    if (saveClick) {
      return true;
    } else {
      if (keyTitle.currentState!.text.length == 0) {
        super.onBack();
        return;
      }
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (_) {
            return CustomDialog(
              content: '保留此次编辑',
              cancelContent: "不保留",
              confirmContent: "保留",
              dismissCallback: () async {
                await BoostNavigator.instance.pop();
                super.onBack();
              },
              confirmCallback: () async {
                await BoostNavigator.instance.pop();
                _onSubmit();
              },
            );
          },
          useRootNavigator: false);
    }
    // return true;
  }

  _onSubmit() {
    saveClick = true;
    if (keyTitle.currentState!.text.length == 0) {
      ToastUtils().showText("案例标题不可为空");
      return;
    }
    Navigator.of(context).pop({
      "title": keyTitle.currentState!.text,
      "content": keyContent.currentState!.text,
      "images": imgList.join(','),
    });
  }
}
